import Vue from 'vue';
import Router from 'vue-router';
import api from '../api/api';
import  {MessageBox}  from 'element-ui';
import { resolve } from 'path';

Vue.use(Router);

function ifHasRight(path,data) {
    var path = path.substr(1);
    var dataKeys = Object.keys(data);
    for(var i=0,j=dataKeys.length;i<j;i++){
        var lis = data[dataKeys[i]];
        var lisKeys = Object.keys(lis);
        for(var m=0,n=lisKeys.length;m<n;m++){
            if(path == lisKeys[m] ){
                return true;
            }
        }
    }
    return false;
}

function checkLogin(to,from,next){
    var userInfo = {
        sUserName:localStorage.getItem('sUserName'),
        iAccountId:localStorage.getItem('iAccountId')
    }
    api.getUrlRightList(userInfo).then((res) => {
        if(res.ret==0){
            if(ifHasRight(to.path,res.content)){
                next();
            }else{
                next('/noright');
            }
        }else{
            next('/login');
        }
    }).catch(e => {
        MessageBox.alert('登录失败请稍后重试',{
            confirmButtonText: '确定'
        });
    })
}

export default new Router({
    routes: [
        {
            path: '/',
            redirect: '/readme'
        },
        {
            path: '/readme',
            component: resolve => require(['../components/common/Home.vue'], resolve),
            children:[
                {
                    path: '/',
                    component: resolve => require(['../components/page/Readme.vue'], resolve)
                },
                {
                    path: '/atomcategorylist',
                    component: resolve => require(['../components/page/AtomCategorylist.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next);
                    // }
                },
                {
                    path: '/atomlist',
                    component: resolve => require(['../components/page/Atomlist.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next);
                    // }
                },
                {
                    path: '/phraseList',
                    component: resolve => require(['../components/page/phraseList.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next);
                    // }
                },
                {
                    path: '/paragraphList',
                    component: resolve => require(['../components/page/paragraphList.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next)
                    // }
                },
                {
                    path: '/articleList',
                    component: resolve => require(['../components/page/ArticleList.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next)
                    // }
                },
                {
                    path: '/newsList',
                    component: resolve => require(['../components/page/NewsList.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next)
                    // }
                },
                {
                    path: '/rightconfig',
                    component: resolve => require(['../components/page/RightConfig.vue'], resolve),
                    // beforeEnter: (to, from, next) => {
                    //     var code = checkLogin(to, from, next);
                    // }
                },
                {
                    path: '/noright',
                    component: resolve => require(['../components/page/NoRight.vue'], resolve)
                }
            ]
        }
        // {
        //     path: '/login',
        //     component: resolve => require(['../components/page/Login.vue'], resolve)
        // }
    ]
})
